package com.gbanker.goldwallet.goldprice.netWork;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;

import java.lang.reflect.Type;
import java.util.HashMap;

/**
 * Created by 丁玉松 on 16/8/25.
 */

public abstract class YYIBaseRequest<ParsedResult> {

    private static final OkHttpClient sHttpClient = new OkHttpClient();
    private HashMap<String,String> mParamsMap;

    public YYIBaseRequest() {
        mParamsMap = new HashMap<String, String>();
    }
    protected abstract String httpMethod();

    protected abstract void insertParams(HashMap<String,String> params);

    protected void addRequestHeader(Context context, Request.Builder requestBuilder){
        requestBuilder.addHeader("channel","appStore");
        requestBuilder.addHeader("platform","ANDROID");
        requestBuilder.addHeader("deviceId","123");
        requestBuilder.addHeader("isEmulator","0");
        requestBuilder.addHeader("manufacture","android");
        requestBuilder.addHeader("osVersion","7.0");
        requestBuilder.addHeader("scene","ANDROID");
        requestBuilder.addHeader("version","0");
        requestBuilder.addHeader("userAgent","GoldPrice/1.0.0");
    }

    //定义一个抽象方法需要子类实现
    protected abstract GbResponse<ParsedResult> parseResponse(GbResponse response);

    public GbResponse<ParsedResult> makeRequest(Context context){
        String method = httpMethod();

        mParamsMap.clear();
        insertParams(mParamsMap);

        Gson json = new Gson();

        Type type = new TypeToken<HashMap<String,String>>(){}.getType();
        String jsonParams1 = new Gson().toJson(mParamsMap , type);

        TypeToken token = new TypeToken<HashMap<String,String>>(){};
        Type type1 = token.getType();
        String jsonParams2 = json.toJson(mParamsMap , type1);

        String jsonParams = new Gson().toJson(mParamsMap,new TypeToken<HashMap<String,String>>() {
        }.getType());

        RequestBody rb = RequestBody.create(MediaType.parse("application/POST; charset=utf-8"),jsonParams);
        Request.Builder reqBuilder = new Request.Builder().addHeader("accept","*/*")
                .addHeader("connection", "Keep-Alive")
                .addHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)")
                .url()
    }


}
